package edu.uoc.tdp.dal;

import edu.uoc.tdp.common.EIssueException;
import edu.uoc.tdp.i18n.TDPLanguageConstants;
import edu.uoc.tdp.i18n.TDPLanguageUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.sql.Statement;

/**
 * @author fjaumot
 */
public class ManagementDb {
    
    protected Connection conexion = null;
    
    public void connectDB() throws EIssueException {
        String url = "";
        String usuario = "";
        String clave ="";
   
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
            throw new EIssueException(TDPLanguageUtils.getMessage
                    (TDPLanguageConstants.ERR_DRIVER));
        }
        
        try {
            Properties prop = new Properties();
            final String propertiesFile = new File("").getAbsolutePath() + "/src/properties/configuration.properties";
            prop.load(new FileInputStream(propertiesFile));
            url = prop.getProperty("url");
            usuario = prop.getProperty("username");
            clave = prop.getProperty("password");
        } catch (java.io.FileNotFoundException e) {
            System.out.println(e.getMessage());
            throw new EIssueException(TDPLanguageUtils.getMessage
                        (TDPLanguageConstants.ERR_NOT_FOUND));
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
            throw new EIssueException(TDPLanguageUtils.getMessage
                        (TDPLanguageConstants.ERR_IN_FILE));
        }
       
        try {
            conexion = DriverManager.getConnection(url, usuario, clave);
        } catch (SQLException sqle) {
            System.out.println(sqle.getMessage());
            throw new EIssueException(TDPLanguageUtils.getMessage
                        (TDPLanguageConstants.ERR_CONNECTION));
        } catch (Exception e) {
            System.out.println(e.getMessage());
            throw new EIssueException(TDPLanguageUtils.getMessage
                            (TDPLanguageConstants.ERR_CONNECTION));
        }
    }
    
     public Statement createCommand() throws EIssueException {
        Statement sentencia = null;
        try {
            sentencia = conexion.createStatement();
        } catch (SQLException sqle) {
            System.out.println(sqle.getMessage());
            throw new EIssueException(TDPLanguageUtils.getMessage
                            (TDPLanguageConstants.ERR_SQL));
        } catch (Exception e) {
            System.out.println(e.getMessage());
            throw new EIssueException(TDPLanguageUtils.getMessage
                    (TDPLanguageConstants.ERR_CONNECTION));
        }
        return sentencia;
    }
     
     public PreparedStatement prepareCommand(String ps) throws EIssueException{
         PreparedStatement command = null;
        try {
            command = conexion.prepareStatement(ps, ResultSet.TYPE_SCROLL_INSENSITIVE, 
                        ResultSet.CONCUR_UPDATABLE);
        } catch (SQLException sqle) {
            System.out.println(sqle.getMessage());
            throw new EIssueException(TDPLanguageUtils.getMessage
                            (TDPLanguageConstants.ERR_SQL));
        } catch (Exception e) {
            System.out.println(e.getMessage());
             throw new EIssueException(TDPLanguageUtils.getMessage
                    (TDPLanguageConstants.ERR_CONNECTION));
        }
        
        return command;
    }
     
      public void closeDb() throws EIssueException{
        try {
            conexion.close(); //cierra la conexion
        } catch (SQLException sqle) {
            System.out.println(sqle.getMessage());
             throw new EIssueException(TDPLanguageUtils.getMessage
                    (TDPLanguageConstants.ERR_STOP));
        } catch (Exception e) {
            System.out.println(e.getMessage());
             throw new EIssueException(TDPLanguageUtils.getMessage
                    (TDPLanguageConstants.ERR_STOP));
        }
    }
      
    public Connection getConnection() {
        return conexion;
    }
    
    public void setConnection(Connection pConexion) {
        conexion = pConexion;
    }
    
}
